package com.zrrd.jt_admin.shop.category.service.impl;

import com.zrrd.jt_admin.common.bo.CategoryBo;
import com.zrrd.jt_admin.common.entity.Category;
import com.zrrd.jt_admin.common.vo.CategoryVo;
import com.zrrd.jt_admin.shop.category.mapper.CategoryMapper;
import com.zrrd.jt_admin.shop.category.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Zhangyk
 * @since 2024-07-01
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Override
    public CategoryVo findAll() {
        CategoryVo categoryVo = new CategoryVo();
        List<Category> categoryList = lambdaQuery().eq(Category::getCategoryId,0).list();
        List<CategoryBo> categoryBoList1 = categoryList.stream().map(category -> {
            CategoryBo categoryBo = new CategoryBo();
            BeanUtils.copyProperties(category, categoryBo);
            List<Category> categoryList1 = lambdaQuery().eq(Category::getId, category.getId())
                    .list();
            List<CategoryBo> categoryBoList = categoryList1.stream().map(category1 -> {
                CategoryBo categoryBo1 = new CategoryBo();
                BeanUtils.copyProperties(category1, categoryBo1);
                List<Category> categoryList2 = lambdaQuery().eq(Category::getId, category.getId())
                        .list();
                List<CategoryBo> categoryBoList2 = categoryList2.stream().map(category2 -> {
                    CategoryBo categoryBo2 = new CategoryBo();
                    BeanUtils.copyProperties(category2, categoryBo2);
                    categoryBo2.setChild(null);
                    return categoryBo2;
                }).collect(Collectors.toList());
                categoryBo1.setChild(categoryBoList2);
                return categoryBo1;
            }).collect(Collectors.toList());
            categoryBo.setChild(categoryBoList);
            return categoryBo;
        }).collect(Collectors.toList());
        categoryVo.setList(categoryBoList1);
        return categoryVo;
    }
}
